package com.springboot.controller;

import com.springboot.dto.OrderDoctorList;
import com.springboot.dto.PatientOrderInfo;
import com.springboot.model.MessageInform;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.PatientMyInformService;
import com.springboot.service.PatientMyOrderService;
import com.springboot.service.SuccessOrderInfoService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
public class PatientOrderRecordController {
    @Autowired
    PatientMyOrderService patientMyOrderService;

    @Autowired
    UserLoginService userLoginService;

    @Autowired
    SuccessOrderInfoService successOrderInfoService;

    @Autowired
    PatientMyInformService patientMyInformService;

    /**
     * 患者点击预约记录时
     * @param phone
     * @return orderDoctorLists

     * @date 2020-12-20
     */
    @CrossOrigin
    @RequestMapping(value = "/patientorderrecord")
    public @ResponseBody
    ArrayList<OrderDoctorList> patientOrderRecord(@RequestParam("unitphone") String phone) {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(phone);
        Integer userId = userLoginForm.getUserId();
        ArrayList<OrderDoctorList> orderDoctorLists = patientMyOrderService.showOrderDoctors(userId);
        System.out.println(userId+" 正在查看我的预约");
        return orderDoctorLists;
    }

    /**
     * 患者取消预约响应,并且向三种用户发送通知（正验证）
     * @param bookId
     * @return

     * @date 2020-12-20
     */
    @CrossOrigin
    @RequestMapping(value = "/patientcancelorder")
    public @ResponseBody
    String patientCancelOrder(@RequestParam("bookId") Integer bookId) {
        //先查找，把order表单保存在内存中，再在数据中删除order表单
        // 因为下面还需要给三种角色发通知需要用到orderForm的值
        OrderForm orderForm =new OrderForm();
        try {
            System.out.println("bookId="+bookId);
            orderForm = patientMyInformService.findOrderFormByBookId(bookId);
        }
        catch (Exception e)
        {
            System.out.println("patientMyInformService.findOrderFormByBookId(bookId) 出错");
        }
        //取消预约
        int resultInt = patientMyOrderService.cancelOrderByBookId(bookId);
        String resultString="";
        System.out.println(resultInt);
        if(resultInt==1)
        {

            resultString = "{\"success\":\"True\"}";
            //resultString = "True";
        }
        else {
            resultString = "{\"success\":\"False\"}";
            //resultString = "False";
        }

        //取消预约时，给三种角色发消息
        try {
            String bootTime = orderForm.getBootTime();
            Integer patientId = orderForm.getPatientId();
            Integer doctorId = orderForm.getDoctorId();
            MessageInform messageInform = new MessageInform();
            messageInform.setBookDate(orderForm.getBootDate());
            messageInform.setBookTime(bootTime);
            messageInform.setPatientId(patientId);
            messageInform.setDoctorId(doctorId);
            Integer messageMark = 3;
            messageInform.setMessageMark(messageMark);
           try {
               Integer messageId = patientMyInformService.insertMessage(messageInform);
               patientMyInformService.insertAssistantMessageInformByDoctorId(doctorId,messageId);
           }
           catch (Exception e)
           {
               System.out.println("取消预约时，消息发送失败");
           }
        }
        catch (Exception e)
        {
            System.out.println("PatientOrderRecordController 出错");
            System.out.println("取消预约时，消息发送失败");
        }
        finally {
            return resultString;
        }
    }
}
